﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using SapaFinance.DTO;


namespace SapaFinance.UI.ActionFilters
{
    public class HandleRecordNotFoundAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var viewResult = filterContext.Result as ViewResult;
            if (viewResult != null)
            {
                var model = viewResult.ViewData.Model as RequestDTO;
                if (model != null)
                {
                    //if (!model.BudgetItems.Any())
                    //{
                    //    filterContext.Result = new ResourceNotFoundResult("В БД список бюджетных статей пуст");
                    //    filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);

                    //}
                    //if (!model.Currency.Any())
                    //{
                    //    filterContext.Result = new ResourceNotFoundResult("В БД нет списка валют");
                    //    filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);

                    //}

                }
            }
        }
    }

    public class ResourceNotFoundResult : ActionResult
    {
        public ResourceNotFoundResult()
        {
            Message = "Нет данных";
        }

        public ResourceNotFoundResult(string message)
        {
            Message = message;
        }

        public string Message { get; private set; }

        public override void ExecuteResult(ControllerContext context)
        {
            throw new HttpException(Message);
        }
    }
}